package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.GuanListAdapter;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.GuanModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的贯
 */
public class MyGuanActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener, PullToRefreshBase.OnRefreshListener {

    private ImageView img_title;
    private TextView text_title, text_menu,tv_tixian;

    private TextView text_guan_num;
    private PullToRefreshListView list_guan;

    private GuanListAdapter guanListAdapter;
    private String TAG = "MyGuanActivity";

    private List<GuanModel> mData;
    private int page = 1;
    private LoadingDialog loadingDialog;
    private boolean isLoadMore = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_guan);
        Tools.changeSystemBar(this);
        initViews();
    }

    @Override
    public void findViewByIds() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        tv_tixian = (TextView) findViewById(R.id.tv_tixian);
        text_title.setText(R.string.guan_title);
        text_guan_num = (TextView) findViewById(R.id.text_guan_num);
        list_guan = (PullToRefreshListView) findViewById(R.id.list_guan);
        list_guan.setMode(PullToRefreshBase.Mode.BOTH);
        list_guan.setOnRefreshListener(this);
        text_guan_num.setText(getIntent().getStringExtra("tmGuanCount"));
    }

    @Override
    public void initListener() {

        img_title.setOnClickListener(this);
        tv_tixian.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mData = new ArrayList<>();
        guanListAdapter = new GuanListAdapter(this);
        list_guan.setAdapter(guanListAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadGuan();
    }

    private void loadGuan() {
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("请稍候");
        loadingDialog.showDialog();
        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("page", "" + page);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.COUPON_USE, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    Log.e(TAG, "onSuccess response.result  " + responseInfo.result);
                    JSONObject object = new JSONObject(responseInfo.result);
                    String code = object.getString("code");
                    if (code.equalsIgnoreCase("100")) {
                        JSONArray list = object.getJSONArray("list");
                        if (list.length() > 0) {
                            if (isLoadMore) {
                                isLoadMore=false;
                            } else {
                                mData.clear();
                            }
                            for (int i = 0; i < list.length(); i++) {
                                GuanModel model = new GuanModel();
                                JSONObject value = list.getJSONObject(i);
                                int count = value.getInt("tgrCount");
                                if (count == 0) {
                                } else {
                                    model.setTgrId(value.getInt("tgrId"));
                                    model.setTgrCount(value.getInt("tgrCount"));
                                    model.setTgrAddTime(new SelfUtils().getDate(value.getString("tgrAddtime")));
                                    model.setTgrType(value.getInt("tgrType"));
                                    mData.add(model);
                                }
                            }
                            guanListAdapter.setData(mData);
                            guanListAdapter.notifyDataSetChanged();
                        } else {
                            if (isLoadMore) {
                                isLoadMore=false;
                            }
                            showMsg("您还没有贯使用记录");
                        }
                        list_guan.onRefreshComplete();

                    } else {
                        if (isLoadMore) {
                            isLoadMore=false;
                            page--;
                        }
                        String msg = object.getString("message");
                        showMsg(msg);
                        list_guan.onRefreshComplete();
                    }
                } catch (Exception e) {
                    if (isLoadMore) {
                        isLoadMore=false;
                        page--;
                    }
                    e.printStackTrace();
                    showMsg("解析异常");
                    list_guan.onRefreshComplete();
                }

                loadingDialog.dismiss();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                showMsg("当前网络不可用，请稍后再试");
                if (page > 1) {
                    page--;
                }
                list_guan.onRefreshComplete();
                loadingDialog.dismiss();

            }
        });

        //  list_guan.onRefreshComplete();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.img_title:
                finish();
                break;
            case R.id.tv_tixian:
                Intent intent = new Intent(this,CashingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        if (refreshView.isHeaderShown()) {
            page = 1;
            loadGuan();
            isLoadMore=false;
        } else if (refreshView.isFooterShown()) {
            page++;
            isLoadMore=true;
            loadGuan();
            list_guan.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载");
            list_guan.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载中...");
            list_guan.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载");
        }
    }
}
